using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Evolution.Engine.Interfaces;
using Evolution.Engine.Managers;

namespace Evolution.Engine.Objects
{
    public class TextureQuad : BaseObject, IRenderable, ILoadable
    {
		protected string _Asset;
        protected Texture2D _Texture;
        protected VertexBuffer _Vertices;
        protected IndexBuffer _Indices;
        protected VertexDeclaration _Declaration;

        protected float _Width;
        protected float _Height;

        public float Height { get { return _Height; } set { _Height = value; } }
        public float Width { get { return _Width; } set { _Width = value; } }

        public TextureQuad(string textureAsset, string name)
            : base(name)
        {
			_Asset = textureAsset;
            _Width = 0.5f;
            _Height = 0.5f;
		}

		public override void LoadContent()
        {
            _Texture = ResourceManager.Instance.LoadContent<Texture2D>(_Asset);

			VertexPositionTexture[] verts = {
				new VertexPositionTexture(new Vector3(-_Width, _Height, 0), new Vector2(0, 0)),
				new VertexPositionTexture(new Vector3(_Width, _Height, 0), new Vector2(1, 0)),		
				new VertexPositionTexture(new Vector3(-_Width, -_Height, 0), new Vector2(0, 1)),	
				new VertexPositionTexture(new Vector3(_Width, -_Height, 0), new Vector2(1, 1))
			};

			int[] inds = { 0, 1, 2, 1, 3, 2 };

			_Vertices = new VertexBuffer(
                GameEngine.Instance.Device, typeof(VertexPositionTexture), 4,
				ResourceUsage.WriteOnly, ResourceManagementMode.Automatic
			);

			_Vertices.SetData<VertexPositionTexture>(verts, 0, 4, SetDataOptions.None);

			_Indices = new IndexBuffer(
                GameEngine.Instance.Device, typeof(int), 6,
				ResourceUsage.WriteOnly, ResourceManagementMode.Automatic
			);

			_Indices.SetData<int>(inds, 0, 6, SetDataOptions.None);

            _Declaration = new VertexDeclaration(GameEngine.Instance.Device, VertexPositionTexture.VertexElements);
		}

		public virtual void Render(GameTime gameTime) 
        {
            bool alphaTest = false;
            bool alphaBlend = false;
            CompareFunction alhpaFunc = CompareFunction.Always;

            if (_AlphaCheck)
            {
                alphaTest = GameEngine.Instance.Device.RenderState.AlphaTestEnable;
                alphaBlend = GameEngine.Instance.Device.RenderState.AlphaBlendEnable;
                alhpaFunc = GameEngine.Instance.Device.RenderState.AlphaFunction;

                GameEngine.Instance.Device.RenderState.AlphaFunction = CompareFunction.NotEqual;
                GameEngine.Instance.Device.RenderState.AlphaBlendEnable = true;
                GameEngine.Instance.Device.RenderState.AlphaTestEnable = true;
            }

            GameEngine.Instance.Device.Indices = _Indices;

            GameEngine.Instance.Device.VertexDeclaration = _Declaration;

            GameEngine.Instance.Device.Textures[0] = _Texture;
            GameEngine.Instance.Device.Vertices[0].SetSource(_Vertices, 0, VertexPositionTexture.SizeInBytes);

            GameEngine.Instance.Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);

            if (_AlphaCheck)
            {
                GameEngine.Instance.Device.RenderState.AlphaTestEnable = alphaTest;
                GameEngine.Instance.Device.RenderState.AlphaBlendEnable = alphaBlend;
                GameEngine.Instance.Device.RenderState.AlphaFunction = alhpaFunc;
            }
		}
    }
}